<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\CouponService;
use Illuminate\Http\Request;

class CouponController extends BaseController
{
    //会员劵

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $uid = $request->post('uid', 0);
        $res = (new CouponService())->getIndex($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //模板列表
    public function tmpIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new CouponService())->tmpIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //模板选项
    public function tmpOption(Request $request)
    {
        $res = (new CouponService())->tmpOption();
        return $this->toJson(0, '', $res['data']);
    }

    //模板列表
    public function tmpAdd(Request $request)
    {
//        $this->validate($request, [
//            'type' => 'required',
//            'quata' => 'required',
//            'expire' => 'required',
//            'reduce' => 'required',
//        ], [
//            'type.required' => '类型必须的',
//            'quata.required' => '面额必须的',
//            'expire.required' => '有效天数必须的',
//            'reduce.required' => '减免额度必须的',
//        ]);
        $pas['type'] = $request->post('type', 0);
        $pas['quata'] = $request->post('quata', 0);
        $pas['scene'] = $request->post('scene', 0);
        $pas['full'] = $request->post('full', 0);
        $pas['reduce'] = $request->post('reduce', 0);
        $pas['opentime'] = $request->post('opentime', 0);
        $opentime = $pas['opentime'] ? time() + 3600 * 24 * $pas['opentime'] : time();
        $pas['opentime'] = date('Y-m-d H:i:s', $opentime);
        $pas['expire'] = $request->post('expire', 0);
        if (!$pas['type']) {
            return $this->toJson(-2, '类型必须的');
        }
        if (!$pas['quata']) {
            return $this->toJson(-2, '面额必须的');
        }
        if (!$pas['expire']) {
            return $this->toJson(-2, '有效天数必须的');
        }
        if (!$pas['reduce']) {
            return $this->toJson(-2, '减免额度必须的');
        }
        $res = (new CouponService())->tmpAdd($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //模板删除
    public function tmpDel(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//        ], [
//            'id.required' => 'ID必须的',
//        ]);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = (new CouponService())->tmpDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    protected $filepath = 'weixin.json';

    //前端显示配置
    public function getSwitch()
    {
        $d = file_get_contents($this->filepath);
        if (!$d) {
            return $this->toJson(-2, '没有配置');
        }
        $d = json_decode($d, true);
        return $this->toJson(0, '', $d['coupon_switch']);
    }

    //前端显示配置
    public function setSwitch(Request $request)
    {
        $v = $request->post('v', 0);
        if (!in_array($v, [1,2])) {
            return $this->toJson(-2, '参数错误');
        }
        $d = file_get_contents($this->filepath);
        if (!$d) {
            return $this->toJson(-2, '没有配置');
        }
        $d = json_decode($d, true);
        $d['coupon_switch'] = $v - 1;
        $d2 = json_encode($d, 384);
        file_put_contents($this->filepath, $d2);
        return $this->toJson();
    }
}